//
//  JumpChooseViewController.m
//  GetPower
//
//  Created by 周博 on 2017/6/9.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "JumpChooseViewController.h"

@interface JumpChooseViewController ()<UITextViewDelegate>
{
    NSInteger _buttonTag;
    NSString *_chooseItem;
    NSString *_chooseId;
}


@property (nonatomic,strong) UIView *bigView;

@property (nonatomic,strong) UIView *topView;

@property (nonatomic,strong) UIView *centerView;

@property (nonatomic,strong) UIView *bottomView;

@property (nonatomic,strong) UIButton *bottomButton;

@property (nonatomic,strong) NSArray *dataArray;

@end

@implementation JumpChooseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initializeDataArray];
    [self creatJumpViewIsShow:NO];
    [self addNoticeForKeyboard];
}

#pragma mark - UISetting

- (void)creatJumpViewIsShow:(BOOL)isShow{
    if (!_bigView) {
        _bigView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenSize.height - 100, kScreenSize.width, 100)];
    }
    
    //头部View
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _bigView.sizeWidth, 50)];

        UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, _topView.sizeHeight)];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton setTitleColor:k9f9f9f forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *sureButton = [[UIButton alloc] initWithFrame:CGRectMake(_topView.sizeWidth - 80, 0, 80, _topView.sizeHeight)];
        [sureButton setTitle:@"确定" forState:UIControlStateNormal];
        [sureButton setTitleColor:kGreenColor forState:UIControlStateNormal];
        [sureButton addTarget:self action:@selector(sureButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [_topView addSubview:cancelButton];
        [_topView addSubview:sureButton];
        
        UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, _topView.sizeHeight-1, _topView.sizeWidth, 1)];
        lineImageView.backgroundColor = kd6d6d6;
        [_topView addSubview:lineImageView];
        
    }
    _topView.backgroundColor = kWhiteColor;
    
    [_bigView addSubview:_topView];
    CGFloat lines = 0;
    //中间View
    if (!_centerView) {
        _centerView = [[UIView alloc] initWithFrame:CGRectMake(0, _topView.allHeight, _bigView.sizeWidth, 80)];
        
        _centerView.backgroundColor = kWhiteColor;

        //创建中间按钮和输入框
        //按钮
        CGFloat width = 0;
        UIButton *button;
        for (NSInteger i = 0; i < _dataArray.count; i ++) {
            NSString *nameStr = [NSString stringWithFormat:@"  %@  ",_dataArray[i][@"name"]];
            CGSize titleSize = [nameStr boundingRectWithSize:CGSizeMake(kScreenSize.width - 40, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size;
            
            if (width + 15 + titleSize.width + 5 >= kScreenSize.width) {
                lines ++;
                width = 0;
            }
            
            button = [[UIButton alloc] initWithFrame:CGRectMake(15 + width, 10 + (10 + 30) *lines,titleSize.width, 30)];
            width += 15 + titleSize.width;
            button.backgroundColor = kWhiteColor;
            button.layer.masksToBounds = YES;
            button.layer.borderWidth = 1;
            button.layer.borderColor = [kColorFrom0x(0xb7b7b7) CGColor];
            button.layer.cornerRadius = 15;
            [button setTitle:_dataArray[i][@"name"] forState:UIControlStateNormal];
            [button setTitleColor:kColorFrom0x(0x808080) forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            button.tag = 1000 + i;
            [button addTarget:self action:@selector(labelsButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            
            [_centerView addSubview:button];
            _centerView.frame = CGRectMake(0, _topView.allHeight, _bigView.sizeWidth, button.allHeight+ 10);

        }
        [_bigView addSubview:_centerView];
    }
    
    _bigView.frame = CGRectMake(_bigView.orginX, kScreenSize.height - _centerView.allHeight, _bigView.sizeWidth, _centerView.allHeight);
    
    [self.view addSubview:_bigView];

}


#pragma mark - 点击事件

- (void)labelsButtonClick:(UIButton *)button{

    if (_buttonTag > 0) {
        UIButton *newButton = (UIButton *)[self.view viewWithTag:_buttonTag];
        newButton.layer.borderColor = [kColorFrom0x(0xb7b7b7) CGColor];
        newButton.backgroundColor = kWhiteColor;
        [newButton setTitleColor:kColorFrom0x(0x808080) forState:UIControlStateNormal];
    }
    
    _buttonTag = button.tag;
    UIButton *oldButton = (UIButton *)[self.view viewWithTag:button.tag];
    oldButton.layer.borderColor = [kWhiteColor CGColor];
    oldButton.backgroundColor = kGreenColor;
    [oldButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
    
    NSLog(@"%@",_dataArray[button.tag - 1000][@"name"]);
    NSString *nameString =_dataArray[button.tag - 1000][@"name"];
    NSString *idString = _dataArray[button.tag - 1000][@"id"];
    _chooseItem = nameString;
    _chooseId = idString;
    if ([nameString isEqualToString:@"自定义"]) {
        [self creatJumpViewIsShow:YES];
    }else{
        [self creatJumpViewIsShow:NO];
    }
}

- (void)sureButtonClick:(UIButton *)button{
    if ([BGFunctionHelper isNULLOfString:_chooseItem]) {
        kAlert(@"请选择所在场所");
        return;
    }
//    NSDictionary *dict = @{
//                           @"indexPath" : _indexPath,
//                           @"name" : _chooseItem,
//                           @"id" : _chooseId
//                           };
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"place" object:dict];
    
    [_delegate getShopPlaceInfoShopPlaceString:_chooseItem shopPlaceId:_chooseId];
    
    [self backButtonClick:nil];
}


- (IBAction)backButtonClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}


#pragma mark - 键盘通知
- (void)addNoticeForKeyboard {
    
    //注册键盘出现的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    //注册键盘消失的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

///键盘显示事件
- (void) keyboardWillShow:(NSNotification *)notification {
    //获取键盘高度，在不同设备上，以及中英文下是不同的
    CGFloat kbHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    
    //计算出键盘顶端到inputTextView panel底端的距离(加上自定义的缓冲距离INTERVAL_KEYBOARD)
    CGFloat offset = (_bigView.orginY + kbHeight) - (self.view.frame.size.height - kbHeight);
    
    // 取得键盘的动画时间，这样可以在视图上移的时候更连贯
    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //将视图上移计算好的偏移
    if(offset > 0) {
        [UIView animateWithDuration:duration animations:^{
            self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        }];
    }
}

///键盘消失事件
- (void) keyboardWillHide:(NSNotification *)notify {
    // 键盘动画时间
    double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //视图下沉恢复原状
    [UIView animateWithDuration:duration animations:^{
        self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }];
}

#pragma mark - 初始化数组

- (void)initializeDataArray{
    _dataArray = @[@{ @"id": @"1", @"name": @"高校" },
                   @{ @"id": @"2", @"name": @"影院" },
                   @{ @"id": @"3", @"name": @"办公场所" },
                   @{ @"id": @"4", @"name": @"儿童乐园" },
                   @{ @"id": @"5", @"name": @"数码城" },
                   @{ @"id": @"6", @"name": @"流动点" },
                   @{ @"id": @"7", @"name": @"娱乐场所" },
                   @{ @"id": @"8", @"name": @"地铁" },
                   @{ @"id": @"9", @"name": @"机场|高铁" },
                   @{ @"id": @"10", @"name": @"景区" },
                   @{ @"id": @"11", @"name": @"医院" },
                   @{ @"id": @"12", @"name": @"餐饮酒店" },
                   @{ @"id": @"13", @"name": @"商场" },
                   @{ @"id": @"14", @"name": @"幼儿园" },
                   @{ @"id": @"15", @"name": @"卖场" },
                   @{ @"id": @"16", @"name": @"其它" }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
